<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .wrap {
            width: 400px;
            height: 400px;
            background-color: red;
            margin: 100px;
            overflow: hidden;
            /* position:relative; */
        }

        .sub {
            width: 300px;
            height: 300px;
            background-color: orange;
            margin: 50px;
            overflow: hidden;
            position:relative;
        }

        .txt {
            width: 100px;
            height: 100px;
            background-color: #000;
            margin: 100px;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="sub">
            <div class="txt"></div>
        </div>
    </div>
</body>
<script>
    // offsetLeft offsetTop   获取元素的偏移相对于参照物父元素的偏移 (margin和position)
    // 参照物父元素可以改变 ?

    // 如何确定参照物父元素? 
    // 自当前元素的父元素开始查找各种定位相关的属性(relative,absolute fixed,sticky),如果能找到则改父元素就是当前元素的参照物父元素,找不到则继续向上一层查找,以此类推,都找不到则body就是参照物父元素

    var txt = document.getElementsByClassName("txt")[0];

    console.log(txt.offsetParent);
    console.log(txt.offsetLeft, txt.offsetTop);

</script>

</html>